package eco.lamp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import eco.lamp.bl.LampItem;
import eco.lamp.bl.LampUpgradeItem;
import eco.lamp.bl.LampUtil;

public class LampUpgradeActivity extends Activity {

	LampItem mOldLamp;
	int mUpgradeStep = 0;
	final static int REQUEST_LAMP = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.new_upgrade);

		((Button) findViewById(R.id.btnINCLamp))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						openLampActivity(view, LampUtil.LAMP_TYPE_INCADESCENT);
					}
				});

		((Button) findViewById(R.id.btnFLRLamp))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						openLampActivity(view, LampUtil.LAMP_TYPE_FLUORESCENT);
					}
				});

		((Button) findViewById(R.id.btnLEDLamp))
				.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						openLampActivity(view, LampUtil.LAMP_TYPE_LED);
					}
				});

		updateGiudlineLabel();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelable("OldLamp", mOldLamp);
		outState.putInt("UpgradeStep", mUpgradeStep);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mOldLamp = savedInstanceState.getParcelable("LampItem");
		mUpgradeStep = savedInstanceState.getInt("UpgradeStep");

		updateGiudlineLabel();

	}

	private void updateGiudlineLabel() {
		TextView upgradeLabel = (TextView) findViewById(R.id.upgradeLabel);
		int upgradeLabelID = 0;
		if (mUpgradeStep == 0)
			upgradeLabelID = R.string.strUpgradeFrom;
		else
			upgradeLabelID = R.string.strUpgradeTo;

		upgradeLabel.setText(getString(upgradeLabelID));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (REQUEST_LAMP == requestCode) {
			switch (resultCode) {
			case RESULT_OK:
				Bundle resultLamp = data.getExtras();
				LampItem lamp = resultLamp.getParcelable("Lamp");
				if (0 == mUpgradeStep) {
					mOldLamp = lamp;
					mUpgradeStep++;
					updateGiudlineLabel();
				} else {
					LampUpgradeItem upgrade = new LampUpgradeItem(mOldLamp,
							lamp);
					Bundle result = new Bundle();
					result.putParcelable("LampUpgradeItem", upgrade);
					Intent i = new Intent();
					i.putExtras(result);
					setResult(RESULT_OK, i);
					finish();
				}
				break;

			default:
				break;
			}
		}
	}

	private void openLampActivity(View v, int lampType) {
		Intent i = new Intent(v.getContext(), LampActivity.class);
		i.putExtra("UpgradeStep", mUpgradeStep);
		i.putExtra("LampType", lampType);
		if (mOldLamp != null)
			i.putExtra("LampNumber", mOldLamp.getNumber());
		startActivityForResult(i, REQUEST_LAMP);
	}
}
